var webpack = require("webpack");
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require("path");

var WEBPACK_ENV = process.env.WEBPACK_ENV || "dev";

var getHtmlConfig = function (name, title) {
	return {
		template: './src/view/' + name + '.html',
		filename: 'view/' + name + '.html',
		favicon: './favicon.ico',
		title: title,
		inject: true,
		hash: true,
		chunks: ['common', name]
	};
};

var config = {
	mode: WEBPACK_ENV,
	entry: {
		"common": path.join(__dirname, 'src/page/common/index.js'),
		"index": path.join(__dirname, 'src/page/index/index.js')
	},
	output: {
		path: path.join(__dirname, "dist"),
		publicPath: "/exam/",
		filename: 'js/[name].js'
	},
	externals: {
		'jquery': 'window.jQuery'
	},
	module: {
		rules: [
			{
				test: /\.css$/,
				use: ExtractTextPlugin.extract({
					fallback: "style-loader",
					use: [
						"css-loader",
						{
							loader: "postcss-loader"
						}
					]
				})
			},
			{
				test: /\.styl$/,
				use: ExtractTextPlugin.extract({
					fallback: "style-loader",
					use: [
						"css-loader",
						{
							loader: "postcss-loader"
						},
						"stylus-loader"
					]
				})
			},
			{
				test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/,
				loader: 'url-loader?limit=100&name=resource/[name].[ext]'
			},
			{
				test: /\.string$/,
				loader: 'html-loader',
				query: {
					minimize: true,
					removeAttributeQuotes: false
				}
			},
			{
				test: /\.js$/,
				exclude: /(node_modules|bower_components)/,
				loader: 'babel-loader'
			}
		]

	},
	resolve: {
		alias: {
			node_modules: __dirname + '/node_modules',
			util: __dirname + '/src/util',
			page: __dirname + '/src/page',
			service: __dirname + '/src/service',
			image: __dirname + '/src/image'
		}
	},
	optimization: {
		runtimeChunk: false,
		splitChunks: {
			cacheGroups: {
				common: {
					name: "common",
					chunks: "all",
					minChunks: 2
				}
			}
		},
		minimize: false
	},
	plugins: [
		new ExtractTextPlugin("css/[name].css"),
		new HtmlWebpackPlugin(getHtmlConfig("index", "不忘初心，牢记使用"))
	],
	devServer: {
		port: 8088,
		inline: true,
		host: "0.0.0.0",
		overlay: {
			errors: true
		}
	}
};
module.exports = config;